package elitech.vietnam.weatherfashion.tabs.zoompager;

import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.custom.ZoomImageView;
import elitech.vietnam.weatherfashion.custom.imageviewzoom.ImageViewTouchBase.DisplayType;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.dto.ProductDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;

public class ZoomPagerFragment extends AbstractFragment {

	private ViewPager mPager;
	private ZoomImageView image;
	private MyCustomPagerAdapter mAdapter;
	private List<ProductDTO> products;
	private PostDTO post;
	int page;
	
	public static ZoomPagerFragment init(List<ProductDTO> params, int page) {
		ZoomPagerFragment fragment = new ZoomPagerFragment();
		fragment.products = params;
		fragment.page = page;
		return fragment;
	}
	
	public static ZoomPagerFragment init(PostDTO post) {
		ZoomPagerFragment fragment = new ZoomPagerFragment();
		fragment.post = post;
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_image_zoom, null);
		setupView(view);
		mPager = (ViewPager) view.findViewById(R.id.zoomable_image_pager);
		image = (ZoomImageView) view.findViewById(R.id.zoom_imagepost);
		image.setDisplayType(DisplayType.FIT_TO_SCREEN);
		
		if (products != null && !products.isEmpty()) {
			mAdapter = new MyCustomPagerAdapter(getChildFragmentManager(), products);
			mPager.setAdapter(mAdapter);
			if (page > 0)
				mPager.setCurrentItem(page, false);
		} else if (post != null) {
			image.setVisibility(View.VISIBLE);
			mPager.setVisibility(View.GONE);
			UrlImageViewHelper.setUrlDrawable(image, Const.SERVER_IMAGE_URL + post.getImage(), R.drawable.loading_time);
		}
		return view;
	}
	
	@Override
	public void onDestroyView() {
		mPager = null;
		mAdapter = null;
		super.onDestroyView();
	}
}
